﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using App2Wpf.ViewModels;

namespace App2Wpf.Views
{
    /// <summary>
    /// FormUC.xaml 的交互逻辑 直接挂载Form
    /// </summary>
    public partial class FormUC : System.Windows.Controls.UserControl,ICardUC
    {
        public int ID { get; set; }

        public delegate void closeCardHandle();

        public event closeCardHandle onClose;

        public nameip connectNameip { get; set; }

        public System.Net.IPAddress iPAddress;

        App2Wpf.Forms.ViewerF view;

        
        public FormUC(nameip serverinfo)
        {
            InitializeComponent();
            initLoading();
            connectNameip = serverinfo;
        }

        public void initLoading()
        {
            LoadingG.Visibility = Visibility.Visible;
        }

        public void StartWork()
        {
            this.iPAddress = System.Net.IPAddress.Parse(connectNameip.ip);
            view = new Forms.ViewerF(iPAddress, connectNameip);
            view.OnConnect += () =>
            {
                Dispatcher.BeginInvoke(new Action(() => LoadingG.Visibility = Visibility.Collapsed));
            };
            cardTitle.Text = view.VirtualDeskForm.Text;
            FormHost.Child = view.VirtualDeskForm;
        }



        public RelayCommand CloseSmallCard
        {
            get => new RelayCommand(() => {
                onClose();
                //viewWindow.session.CloseSession();
            }
            );
        }

        private void Close_Button_Click(object sender, RoutedEventArgs e)
        {
            view.session.CloseSession();
            ServiceLocator.rightWrapVM.removeCard(this);
        }
    }
}
